package com.ruoyi.location.domain.team;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.location.exception.LocationRuntimeException;
import com.ruoyi.location.repository.TeamRepository;
import com.ruoyi.location.repository.generator.entity.LocationReportRecord;
import com.ruoyi.location.repository.generator.entity.LocationTeam;
import com.ruoyi.location.repository.generator.entity.LocationUserInfo;
import com.ruoyi.location.domain.quickreport.util.RandomStringUtil;
import com.ruoyi.location.vo.req.TeamCreateReq;
import com.ruoyi.location.vo.req.TeamUpdateReq;
import com.ruoyi.location.vo.resp.UserLocationLatestResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TeamDomainService {
    @Autowired
    private TeamRepository teamRepository;

    @Value("${location.domain}")
    private String domain;

    public LocationTeam create(TeamCreateReq req) {
        LocationTeam team = teamRepository.create(req);
        teamRepository.relationUserTeam(ShiroUtils.getUserId(), team.getId());
        return team;
    }

    public LocationTeam update(TeamUpdateReq req) {
        return teamRepository.update(req);
    }

    public void delete(Long teamId) {
        // 1 删除team
        // 2 删除team 和 user的关系
        teamRepository.deleteTeam(teamId);
    }

    public void quit(Long teamId) {
        Long userId = ShiroUtils.getUserId();
        //  删除team 和 user的关系
        teamRepository.quitTeam(userId, teamId);
    }

    public String createShareLink(Long teamId) {
        Long userId = ShiroUtils.getSysUser().getUserId();
        String inviteCode = RandomStringUtil.generateRandomString(8);
        teamRepository.insertInvite(inviteCode, teamId, userId);
        return this.domain + "/location/team/invite?code=" + inviteCode;
    }

    public void joinTeam(Long teamId) {
        Long userId = ShiroUtils.getUserId();
        teamRepository.insertRelUserTeam(userId, teamId);
    }

    public List<UserLocationLatestResp> listLatestLocationByTeamId(Long teamId) {
        List<LocationUserInfo> users = teamRepository.listUserByTeamId(teamId);
        Set<Long> userIds = users.stream().map(LocationUserInfo::getUserId).collect(Collectors.toSet());
        if (!userIds.contains(ShiroUtils.getUserId())) {
            log.info("cur user: {}, userIds: {}", ShiroUtils.getUserId(), JSON.toJSON(userIds));
            throw new LocationRuntimeException("您不是该团队成员");
        }
        List<UserLocationLatestResp> result = new ArrayList<>();
        for (LocationUserInfo u : users) {
            LocationReportRecord record = teamRepository.selectLatestByUserId(u.getUserId());
            UserLocationLatestResp resp = new UserLocationLatestResp();
            resp.setUserId(u.getUserId());
            resp.setName(u.getNickName());
            resp.setAvatar(u.getAvatar());
            resp.setPhoneNum(u.getPhoneNum());
            resp.setLatitude(record.getLatitude());
            resp.setLongitude(record.getLongitude());
            resp.setReportTime(record.getReportTime());
            result.add(resp);
        }
        return result;
    }

}
